//
//  RXFeatureViewController.swift
//  weibo
//
//  Created by uncle-R on 15/12/17.
//  Copyright © 2015年 uncle-R. All rights reserved.
//
//用collectionView制作新特性界面

import UIKit

class RXFeatureViewController: UICollectionViewController {

    private let reuseIdentifier = "Cell"
    //流水布局
    private let layout = UICollectionViewFlowLayout()
    //MARK: - 新特性界面数量
    private let ItemCount = 4
    
    //AppDelegate会调用没有参数的init方法
    init(){
        
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //准备Collection相关的参数
        prepareCollectionView()
        //[UICollectionViewCell class] = UICollectionViewCell.RXFeatureViewController
        //注册collectionCelld的类型.
        //UICollectionViewCell.self 可以理解成拿到UICollectionViewCell 这个类
        //UICollectionViewCell.self 后面 self不是值当前的类.
        self.collectionView?.registerClass(RXCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView?.pagingEnabled = true
        self.collectionView?.bounces = false
        self.collectionView?.showsHorizontalScrollIndicator = false
        
    }

    func prepareCollectionView(){
        //设置布局的大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
    }
    

    //MARK: - 实现数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
   
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        //去缓存池中查找. //已经注册过cell  可以强制拆包
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RXCollectionCell
        
        // 将第几个cell赋值给index
        cell.index = indexPath.item
  
        
        return cell
       
    }
   
     // collectionView显示完毕时调用(在屏幕上看不到的时候会调用) 等到第二个cell出现 第一个正好消失的时候调用
     // collectionView停到某一页
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
    {

        //取出cell组 indexPath的最后一组.///>返回现有的所有cell的indexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        //判断是不是最后一个 cell对应的IndexPath
        if visibleIndexPath.item == ItemCount - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! RXCollectionCell
            cell.starButtonAnimation()
        }   
        
}

//MARK: - 自定义collectionView
class RXCollectionCell :UICollectionViewCell{
    
    var index = 0 {
        //属性监视器, 当index发生改变的时候调用
        didSet{
            //当值发生改变的时候给要给每个Cell的背景图片赋值
            backgImage.image = UIImage(named: "new_feature_\(index + 1)")
            // 每当cell要显示之前都隐藏开始按钮 为了不让cell复用出现 按钮 每次都要隐藏
            startBtn.hidden = true

        }
        
    }
    //加载cell的时候初始化属性
    override init(frame: CGRect) {
        super.init(frame: frame)
      //添加UI控件
        prepareUI()
   
  
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func prepareUI(){
        
        contentView.addSubview(backgImage)
        contentView.addSubview(startBtn)
        
//        backgImage.translatesAutoresizingMaskIntoConstraints = false
//        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //约束控件
//        contentView.addConstraint(NSLayoutConstraint(item: backgImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: backgImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: backgImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: backgImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        ///背景,填充父控件
        backgImage.ff_Fill(self.contentView)
        //头像,水平和父控件重合
        startBtn.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: self.contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
//        
//        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
//        
        
    }
    //MARK: - 设置开始按钮的动画.
    
    func starButtonAnimation(){

        startBtn.hidden = false
        //初始形泰.
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        //使用弹簧效果
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            self.startBtn.transform = CGAffineTransformMakeScale(1,1)
            self.startBtn.transform = CGAffineTransformIdentity ///>取消形变属性.
   
            }) { (_) -> Void in
                
                print("动画完成")
    
        }
    }
    
    //MARK: - 开始按钮被点击
    func startBtnClick(){
        //跳转界面
  (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
        
        
    }

    
   //MARK: - 懒加载
    //背景图片
    
    private lazy var backgImage :UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        
   //开始按钮.
    private lazy var startBtn: UIButton =
    {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮文字
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.hidden = true
        btn .addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
}


